package qdu.edu.cn.liu.get;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.zip.GZIPInputStream;

import static java.nio.charset.StandardCharsets.UTF_8;

/**
 * 获取网页的源码
 * 网页中包含了图片的超链接
 * TODO 最终需要将文件存储到redis中
 * TODO 需要将输入的输入方式改为一个指定格式的文件，该文件来自hive处理的结果
 */
public class RequestURL {
    private String getHtmlContent(URL url) {
        String htmlContentFromInputStream = null;
        HttpURLConnection con = getConnection(url);
        try {
            // 获得网页返回信息码
            int responseCode = con.getResponseCode();

            switch (responseCode) {
                // 从InputStream中解析出html内容（源码）
                case 200:
                    htmlContentFromInputStream = getHTMLContentFromInputStream(con.getInputStream());
                    break;
                // 请求失败
                case -1:
                    System.out.println(url.toString() + " : connection is failure...");
                    break;
                default:
                    System.out.println("请求失败:get response code: " + responseCode);
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("error: " + url.toString());
        } finally {
            con.disconnect();
        }
        return htmlContentFromInputStream;
    }

    public String getHtmlContent(String searchWords) {
        String htmlContent = null;
        try {
            URL rUrl = new URL("https://image.baidu.com/search/index?tn=baiduimage&istype=2&ie=utf-8&fm=index&pos=history&word=" + searchWords);
            htmlContent = getHtmlContent(rUrl);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return htmlContent;
    }

    private HttpURLConnection getConnection(URL url) {
        HttpURLConnection con = null;
        try {
            con = (HttpURLConnection) url.openConnection();
            con.setRequestProperty("Connection", "keep-alive");
            con.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
            con.setRequestProperty("Accept-Encoding", "gzip, deflate, br");
            con.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.9");
            con.setRequestProperty("Cache-Control", "max-age=0");
            con.setRequestProperty("Content-Type", "text/html; charset=UTF-8");
            con.setRequestProperty("sec-ch-ua", "\" Not;A Brand\";v=\"99\", \"Google Chrome\";v=\"91\", \"Chromium\";v=\"91\"");
            con.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36");// IE代理进行下载
            con.setRequestMethod("GET");
            con.setConnectTimeout(60000);
            con.setReadTimeout(60000);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return con;
    }

    private String getHTMLContentFromInputStream(InputStream inputStream) {
        StringBuilder contentBuffer = new StringBuilder();

        try (
                GZIPInputStream gzip = new GZIPInputStream(inputStream);
                InputStreamReader inputStreamReader = new InputStreamReader(gzip, UTF_8);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader)
        ) {
            String str;
            while ((str = bufferedReader.readLine()) != null)
                contentBuffer.append(str);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return contentBuffer.toString();
    }
}